﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCodeArray
{
    /// <summary>
    /// 题目：https://leetcode-cn.com/problems/two-sum/
    /// </summary>
    public class LeetCode1
    {
        public static int[] TwoSum(int[] nums, int target)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = i + 1; j < nums.Length; j++)
                {
                    if (nums[i] + nums[j] == target)
                        return new int[] { i, j };
                }
            }
            return null;
        }
        public static int[] TwoSum1(int[] nums, int target)
        {
            System.Collections.Generic.Dictionary<int, int> dic = new System.Collections.Generic.Dictionary<int, int>();
            dic.Add(nums[0], 0);
            for (int i = 1; i < nums.Length; i++)
            {
                int tmp = nums[i];
                if (dic.Keys.Contains(target - tmp))
                    return new int[] { dic[target - tmp], i };
                else if (!dic.Keys.Contains(tmp))
                    dic.Add(tmp, i);
            }
            return null;
        }
        public static void Run()
        {
            var nums = new int[] { 3, 2, 4 };
            var result = TwoSum1(nums, 6);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
        }
    }

    /// <summary>
    /// 题目地址 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
    /// </summary>
    public class LeetCode26
    {
        public static int RemoveDuplicates(int[] nums)
        {
            var temp = nums.Distinct().ToArray();
            int cnt = temp.Count();
            for (int i = 0; i < cnt; i++)
            {
                nums[i] = temp[i];
            }
            return cnt;
        }
        public static void Run()
        {
            var nums = new int[] { 1, 1, 2 };
            var result = RemoveDuplicates(nums);
            foreach (var item in nums)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

    /// <summary>
    /// 题目地址 https://leetcode-cn.com/problems/remove-element/
    /// </summary>
    public class LeetCode27
    {
        public static int RemoveElement(int[] nums, int val)
        {
            var tmp = nums.Where(x => x != val).ToArray();
            int total = tmp.Count();
            for (int i = 0; i < total; i++)
            {
                nums[i] = tmp[i];
            }
            return total;
        }
        public static void Run()
        {
            var nums = new int[] { 3, 2, 2, 3 };
            var result = RemoveElement(nums, 3);
            foreach (var item in nums)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

    /// <summary>
    /// https://leetcode-cn.com/problems/search-insert-position/
    /// </summary>
    public class LeetCode35
    {
        public static int SearchInsert(int[] nums, int target)
        {
            for (int i = 0; i < nums.Count(); i++)
            {
                if (nums[i] >= target)
                    return i;
            }
            return nums.Count();
        }
        public static void Run()
        {
            var nums = new int[] { 1, 3, 5, 6 };
            var result = SearchInsert(nums, 5);
            foreach (var item in nums)
            {
                //Console.WriteLine(item);
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}
